<template>
    <transition name="com-fade">
        <div id="picker" v-show="state" @click.self="cancel">
            <transition name="com-move">
                <div class="container" ref="container" :style="containerStyle" v-show="state">
                    <div class='title'>{{ title }}</div>
                    <ul>
                        <li v-for="(item, index) in newList" :key="index" @touchstart="touchStart(index)"
                            @touchend.prevent="choose(item.id)">
                            <h2>{{ item.title }}</h2>
                            <i :style="iconStyle(item.isChoosed, false)"></i>
                        </li>
                    </ul>
                    <div v-if="type === 'radio'" class="button" @click="cancel">{{ $t('cancel') }}</div>
                    <BaseButtons v-else @cancel="cancel" @submit="submit" />
                </div>
            </transition>
        </div>
    </transition>
</template>

<script>
import { mapGetters } from 'vuex';
export default {
    props: {
        selectedNum: {
            default: 0
        },
        list: {
            type: Array
        },
        title: {
            type: String
        },
        // checkbox 多选框  radio 单选框
        type: {
            type: String,
            default: 'radio'
        },
        state: {
            default: false,
            type: Boolean
        }
    },
    data() {
        return {
            newList: this.list
        };
    },
    watch: {
        list(val) {
            this.newList = val;
        },
        state(val) {
            if (val) {
                this.init();
            }
        },
        selectedNum(val) {
            this.init();
        }
    },
    computed: {
        ...mapGetters([
            'imgPath'
        ]),
        containerStyle() {
            return {
                position: 'absolute',
                left: 'var(--emui_dimens_dialog_start)',
                right: 'var(--emui_dimens_dialog_end)',
                bottom: 'var(--emui_dimens_dialog_bottom)'
            };
        }
    },
    methods: {
        choose(index) {
            if (this.type === 'radio') {
                this.$emit('choose', index);
            }
        },
        cancel() {
            console.log("11111111")
            this.$emit('cancel');
            // alert(1)
            // if (this.type === 'radio') {
            //     this.$emit('cancel');
            // } else {
               
            //     // this.$emit('chooseWeekCustom', 'cancel');
            // }
        },
        submit() {
            let weekCustom = [];
            for (let i = 0; i < this.list.length; i++) {
                // weekCustom += (Number(this.list[i].isChoosed) << i);
                // if (this.list[i].isChoosed) {
                    weekCustom.push(this.list[i].isChoosed?1:0)
                // }
            }
            this.$emit('chooseWeekCustom', 'hasChoosed', weekCustom);
        },
        touchStart(index) {
            if (this.type === 'radio') {
                for (let i = 0; i < this.list.length; i++) {
                    this.$set(this.newList[i], 'isChoosed', i === index);
                }
            } else {
                this.$set(this.newList[index], 'isChoosed', !this.newList[index].isChoosed);
            }
        },
        iconStyle(isChoosed, hidden) {
            let backgroundImage;
            let display = hidden ? 'none' : '';
            if (this.type === 'radio') {
                backgroundImage = isChoosed ?
                    require(`../assets/${this.imgPath}ic_Radio_on.png`) :
                    require(`../assets/${this.imgPath}ic_Radio_off.png`);
            } else {
                backgroundImage = isChoosed ?
                    require(`../assets/${this.imgPath}ic_checkbox_on.png`) :
                    require(`../assets/${this.imgPath}ic_checkbox_off.png`);
            }
            return {
                'background-image': `url(${backgroundImage})`,
                'background-size': 'cover',
                display
            };
        },
        init() {
            if (this.type === 'radio') {
                for (let i = 0; i < this.list.length; i++) {
                    this.$set(this.newList[i], 'isChoosed', this.list[i].id == this.selectedNum);
                }
            } else {
              
                let arr = this.selectedNum ? this.selectedNum.split('') : []
                console.log("arr",arr)
                for (var i = 0; i < this.list.length; i++) {

                    this.$set(this.newList[i], 'isChoosed', arr[i] == 1);
                    // for (let j = 0; j < arr.length; j++) {
                    //     arr[j] == this.list[i].id && this.$set(this.newList[i], 'isChoosed', arr[j] == 1);
                    // }

                }
            }
        }
    },
    mounted() {
        this.init();
    }
};
</script>

<style lang='less' scoped>
@import url("../style/public.less");

@media only screen and (min-width: 600px) {
    .container {
        left: calc(25% + 1.2rem) !important;
        right: calc(25% + 1.2rem) !important;
        bottom: 50% !important;
        transform: translateY(50%);
    }
}

#picker {
    .mask();
    .fade();

    &.spread {
        display: flex;
        justify-content: center;
        align-items: center;
    }

    .container {
        min-height: 10rem;
        z-index: 100;
        background-color: var(--emui_popup_bg);
        border-radius: var(--emui_corner_radius_xlarge);
        color: var(--emui_text_primary);

        // .move();
        .title {
            height: 5.6rem;
            padding: 0 2.4rem;
            display: flex;
            justify-content: flex-start;
            align-items: center;
            font-size: var(--emui_text_size_headline8_px);
            .medium();
        }

        ul {
            li {
                height: 4.8rem;
                position: relative;
                padding: 0 2.4rem;
                display: flex;
                justify-content: space-between;
                align-items: center;
                .active(@radius: 0);
                .border(@left: 2.4rem, @right: 2.4rem);

                h2 {
                    font-size: var(--emui_text_size_subtitle2);
                    font-weight: normal;
                }

                i {
                    display: block;
                    box-sizing: border-box;
                    width: 2.4rem;
                    height: 2.4rem;
                }

                &:last-child {
                    .border(0);
                }
            }
            margin-bottom:0.8rem;
        }

        .button {
            height: 4rem;
            margin: .8rem 1.6rem 1.6rem;
            display: flex;
            justify-content: center;
            align-items: center;
            font-size: var(--emui_text_size_button1);
            text-align: center;
            color: var(--emui_accent);
            .medium();
            .active(@radius: 2rem);
        }
    }
}
</style>
